package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter;

import android.content.Context;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.diudiutechnology.www.yadiudiu.R;

import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.view.MyEditText;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.ReleaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.ReleaseModel;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/18.
 * 上传图文详情
 */

public class ReleaseActivityAdapter extends BaseAdapter {

    private Context mContext;

    List<ReleaseModel> model= new ArrayList<>();
    private Integer index = -1;
    ViewHolder holder = null;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    int Judge=0;
    public ReleaseActivityAdapter(Context context, List<ReleaseModel> model,int Judge) {
        this.mContext = context;
        this.model=model;
        this.Judge=Judge;
        options = ImgUtils.imageOpti();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

    @Override
    public int getCount() {
        return model.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public void addItem(int pos, ReleaseModel models){
        this.model.add(pos,models);
        notifyDataSetChanged();
    }
    public void addimg(List<ReleaseModel> models){
       this.model.clear();
        this.model.addAll(models);
        notifyDataSetChanged();
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_release, null);
            holder.upload_photos_img= (ImageView) convertView.findViewById(R.id.upload_photos_img);
            holder.summary_of_housing_text= (MyEditText) convertView.findViewById(R.id.summary_of_housing_text);
            holder.deleting= (LinearLayout) convertView.findViewById(R.id.deleting);
            holder.summary_of_housing_leng= (TextView) convertView.findViewById(R.id.summary_of_housing_leng);
            holder.summary_of_housing_text.setTag(position); // 为输入框设定标签，防止修改时错乱
            holder.summary_of_housing_text.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        index = (Integer) v.getTag();
                    }
                    return false;
                }
            });
            holder.summary_of_housing_text.addTextChangedListener(new MyTextWatcher(holder));
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.summary_of_housing_text.setTag(position);
        }
        holder.summary_of_housing_text.setText(model.get(position).getInstructiona());
        imageLoader.displayImage(Constant.HTTP + model.get(position).getPicimga(), holder.upload_photos_img, options);
        holder.upload_photos_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (model.size()<4){
                    if (Judge==0){
                        ((ReleaseActivity)mContext).processimg(position);
                    }

                }else {
                    ToastUtils.show(mContext, mContext.getResources().getString(R.string.please_illustrations));
                }
            }
        });
        holder.deleting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Judge==0){
                    ((ReleaseActivity)mContext).processdeleting(position);
                }
            }
        });
        return convertView;
    }
    class MyTextWatcher implements TextWatcher {

        private ViewHolder mHolder;

        public MyTextWatcher(ViewHolder holder) {
            mHolder = holder;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {
            // TODO Auto-generated method stub
            if (s.length() > 0) {
                int position = (Integer) mHolder.summary_of_housing_text.getTag();
                System.out.println("stringmd5result.size()="+position);
                model.get(position).setInstructiona(holder.summary_of_housing_text.getText().toString());
                System.out.println("stringmd5result.size()="+ model.get(position).getInstructiona());
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if (s != null && !"".equals(s)) {
                int position = (Integer) mHolder.summary_of_housing_text.getTag();
                model.get(position).setInstructiona(s.toString());
              String temp = s.toString();
                mHolder.summary_of_housing_leng.setText(temp.length()+"/200");
//                int d = temp.indexOf(".");
//                if (d < 0) return;
//                if (temp.length() - d - 1 > 2){
//                    s.delete(d + 3, d + 4);
//                }else if (d==0) {
//                    s.delete(d, d+1);
//                }
            }
        }
    }
    class ViewHolder {
        ImageView upload_photos_img;
        TextView summary_of_housing_leng;
        MyEditText summary_of_housing_text;
        LinearLayout deleting;
    }
}



